package com.example.androidcomplete02;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends ExpandableListActivity {

	private String[] mainTitles;
	private String[][] subTitles;
	private String[] pkgLists;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mainTitles = getResources().getStringArray(R.array.titles);
		pkgLists = getResources().getStringArray(R.array.pkglist);

		subTitles = new String[mainTitles.length][];

		for (int i = 0; i < mainTitles.length; i++) {
			int resId = getResources().getIdentifier("subtitles" + i, "array",
					getPackageName());// subtitles0 ,subtitles1.. 순서대로 array타입의
										// id를 찾음(R파일에 있는 아이디)
			subTitles[i] = getResources().getStringArray(resId);
		}

		/*
		 * for (int i = 0; i < mainTitles.length; i++) { Log.d("Test",
		 * mainTitles[i]); for(int j = 0; j<subTitles[i].length; j++){
		 * Log.d("	Test", subTitles[i][j]); } }
		 */

		MyExpandableListAdapter adapter = new MyExpandableListAdapter();
		setListAdapter(adapter);
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		super.onChildClick(parent, v, groupPosition, childPosition, id);
		Intent intent = new Intent();
		// setClassName
		//package name, class name
		//class name: com.example.androidcomplete02.fragment.FragmemtEx
		intent.setClassName(getPackageName(), getPackageName() + "."
				+ pkgLists[groupPosition] + "."
				+ subTitles[groupPosition][childPosition]);
		startActivity(intent);
		return true;
	}

	class MyExpandableListAdapter extends BaseExpandableListAdapter {

		@Override
		public int getGroupCount() {
			return mainTitles.length;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return subTitles[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return mainTitles[groupPosition];
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return subTitles[groupPosition][childPosition];
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {// collection이면 false
										// array는 고정되어 있으므로 true
			return true;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			TextView tv = new TextView(MainActivity.this);
			tv.setTextSize(30);
			tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
			tv.setPadding(36, 0, 0, 0);
			tv.setText(getGroup(groupPosition).toString());
			return tv;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
			TextView tv = (TextView) inflater.inflate(
					android.R.layout.simple_expandable_list_item_1, null);
			tv.setText(getChild(groupPosition, childPosition).toString());
			return tv;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

	}
}
